<template>
  <div class="login_con">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 登录表单 使用element-ui-->
      <el-form
        label-width="0px"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 登录，重置 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="loginin">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单回到默认值
    reset () {
      this.$refs.loginFormRef.resetFields()
    },
    loginin () {
      this.$refs.loginFormRef.validate(async response => {
        // console.log(response)// 符合规则返回为true,否则为false
        if (!response) return
        const result = await this.$http.post('login', this.loginForm)
        console.log(result) // 是一个对象
        if (result.meta.status !== 200) {
          // return alert('用户名或密码错误，登录失败!')
          return this.$message.error('登陆失败！')
        }
        // console.log(result)// 登录成功返回参数
        this.$message.success('登录成功！')
        // 将用户信息保存到客户端
        window.sessionStorage.setItem('token', result.data.token)
        // 通过编程式导航跳转到后台首页
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_con {
  height: 100%;
  background-color: #2b4b6b;
}
// 登录表单
.login_box {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  height: 400px;
  border-radius: 5px;
  background-color: #fff;
  //   头像
  .avatar_box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 130px;
    height: 130px;
    padding: 10px;
    background-color: #fff;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  //   登录表单
  .login_form {
    position: absolute;
    bottom: 120px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .btn {
      position: absolute;
      right: 20px;
    }
  }
}
</style>
